"""
Calculating most popular species counts is VERY cpu-intensive, so we run this periodic script
to populate the species' lastcount field

"""

#################### Set up Django environment
import sys,os
from datetime import *

os.environ['DJANGO_SETTINGS_MODULE'] ='treedata.settings'

from django.core.management import setup_environ
from treedata import settings
setup_environ(settings)

#################### Import models from Django project

from trees.models import *
from django.contrib.auth.models import User, Group
#################### Start work

species = Species.objects.all()

for s in species:
    thecount = s.plant_set.all().count()
    s.lastcount = thecount
    s.save()
    print s
    print thecount
    print
    

 